/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.cicp.collector.windows;

import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.IOUtils;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.WmiUtil;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-7-4 上午11:09:43          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class WinWMICollector extends Collector {
    private static final String SEPARATOR = " <@> ";

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        String script = this.paramMap.get("SCRIPT");
        String[] args = null;
        int index = script.trim().indexOf(" ");
        if (index > -1) {
            String[] ss = new String[2];
            ss[0] = script.substring(0, index);
            ss[1] = script.substring(index).trim();
            script = ss[0];
            args = new String[] { ss[1] };
        }
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        BufferedReader reader = null;
        String[] propMetas = getPropMeta();
        try {
            reader = WmiUtil.execWmiScript(ip, script, args);
            String readLine = null;
            while ((readLine = reader.readLine()) != null) {
                boolean flag = false;
                if (readLine.indexOf(SEPARATOR) > -1) {
                    flag = true;
                }
                if (flag) {
                    String[] ss = readLine.split(SEPARATOR);
                    ResPropertyValue propertyValueX = new ResPropertyValue();
                    ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[ss.length];
                    for (int i = 0; i < propValues.length; i++) {
                        propValues[i] = ResPropertyValue.createPropValue(ss[i].trim(), propMetas[i]);
                    }
                    propertyValueX.setValues(propValues);
                    values.add(propertyValueX);
                }
            }
        } catch (Exception e) {
            logger.error("failed to collect by WinWMICollector", e);
            throw new CollectorException(e);
        } finally {
            IOUtils.closeQuietly(reader);
        }
        return values;
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "SCRIPT" };
    }

    @Override
    public String[] getPropMeta() {
        return new String[] { "$1", "$2", "$3", "$4", "$5", "$6", "$7", "$8", "$9", "$10" };
    }

}
